<?php

class compiler {

    private $regexVar	 	 =  '(?:\$([A-Za-z0-9]{1,}))';
    private $regexSpace	 	 =  '(?:\s)*';
    //	private $regexBlockName	 =  '([A-Za-z]{1,})';
    //  private $regexParams     =  '((?:[A-Za-z]{1,})="(?:[^>]*)")*';
    private $regexConst  	 =  '((?:[0-9\.]+)|(?:\-[0-9\.]+)|(?:[0-9]+)|(?:\-[0-9]+))';

    private $content;
    private $TplSystem;



    function __construct($tab) {
        $this->content   = $tab['0'];
        $this->TplSystem = $tab['1'];
        $this->TplSystem->foreachVars = (object) NULL;
        $this->parse();
    }

    private function parse() {
        // Search Vars {$var}
        if (preg_match_all('#\{' .$this->regexSpace. $this->regexVar . '((?:(?:\[(?:\"|\')[\'\"A-Za-z0-9]+(?:\"|\')\])+)|(?:(?:\->(?:.*)+))+)?' . $this->regexSpace .'\}#',$this->content, $matchVars)) {
            for ($i=0; $i < count($matchVars[0]); $i++) {
                if (isset($this->TplSystem->vars->{$matchVars[1][$i]})) {
                    $array = (isset($matchVars[2][$i])) ? $matchVars[2][$i] : '';
                    $this->content = str_replace($matchVars[0][$i], '<?php echo $this->vars->'.$matchVars[1][$i].$array.'; ?>', $this->content);
                }
            }
        }


        $this->content =  preg_replace_callback(
                                                "|(\d{2}/\d{2}/)(\d{4})|",
            array($this,"next_year"),
            $this->content);
        $this->content =  preg_replace_callback(
                                                '#<if' .$this->regexSpace.'cond="(.*)"' .$this->regexSpace.'>#',
            array($this,"compile_if_start"),
            $this->content);
        $this->content =  preg_replace_callback(
                                                '#</if>#',
            array($this,"compile_if_end"),
            $this->content);
        $this->content =  preg_replace_callback(
                                                '#<else>#',
            array($this,"compile_else_start"),
            $this->content);
   
        $this->content =  preg_replace_callback(
                                                '#<foreach'.$this->regexSpace .'var="' .$this->regexSpace. $this->regexVar . $this->regexSpace .'"'.$this->regexSpace .'as="' .$this->regexSpace. $this->regexVar . $this->regexSpace .'((?:=>' .$this->regexSpace. $this->regexVar . $this->regexSpace .'))?'.$this->regexSpace .'">#',
            array($this,"compile_foreach_start"),
            $this->content);
        $this->content =  preg_replace_callback(
                                                '#</foreach>#',
            array($this,"compile_foreach_end"),
            $this->content);
        ;
        // Search Foreach Var
        if (preg_match_all('#\{\$foreach\->([A-Za-z0-9]+)((?:\[(?:\"|\')[\'\"A-Za-z0-9]+(?:\"|\')\])+)?\}#',$this->content, $matchFVars)) {
            for ($i=0; $i < count($matchFVars[0]); $i++) {
                if (isset($this->TplSystem->foreachVars->{$matchFVars[1][$i]})) {
                    $array = (isset($matchFVars[2][$i])) ? $matchFVars[2][$i] : '';
                    $this->content = str_replace($matchFVars[0][$i], '<?php echo  $'.$matchFVars[1][$i].$array.' ; ?>', $this->content);
                }
            }
        }

        // Search Var $var
        // Search Vars {$var}
        if (preg_match_all('#' .$this->regexSpace. $this->regexVar . '((?:(?:\[(?:\"|\')[\'\"A-Za-z0-9]+(?:\"|\')\])+)|(?:(?:\->(?:.*)+))+)?' . $this->regexSpace .'#',$this->content, $matchVars)) {
            for ($i=0; $i < count($matchVars[0]); $i++) {
                if (isset($this->TplSystem->vars->{$matchVars[1][$i]})) {
                    $array = (isset($matchVars[2][$i])) ? $matchVars[2][$i] : '';
                    $this->content = str_replace($matchVars[0][$i], '$this->vars->'.$matchVars[1][$i].$array.'', $this->content);
                }
            }
        }

    }

    function next_year($matches) {
        return $matches[1].($matches[2]+1);
    }
    function compile_if_start($matches) {
        $this->parseVars($matches[1], $matches[1]);
        return "<?php if($matches[1]) { ?>";
    }
    function compile_if_end($matches) {
        return "<?php } ?>";
    }
    function compile_else_start($matches) {
        return "<?php } else { ?>";
    }

    function compile_foreach_start($matches) {
        $this->parseVars('$'.$matches[1], $matches[1]);
        $as = (isset($matches[3])) ? $matches[3] : '';
        $this->TplSystem->foreachVars->{$matches[2]} = true;
        return '<?php foreach('.$matches[1].' as  $'.$matches[2].$as.') { ?>';
    }
    function compile_foreach_end($matches) {
        return "<?php } ?>";
    }


    public function fetch() {
        return $this->content;
    }

    private function parseVars($content, &$modify) {
        if (preg_match_all('#' .$this->regexSpace. $this->regexVar . '((?:(?:\[(?:\"|\')[\'\"A-Za-z0-9]+(?:\"|\')\])+)|(?:(?:\->(?:.*)+))+)?' . $this->regexSpace .'#',$content, $matchVars)) {
            for ($i=0; $i < count($matchVars[0]); $i++) {
                if (isset($this->TplSystem->vars->{$matchVars[1][$i]})) {
                    $array = (isset($matchVars[2][$i])) ? $matchVars[2][$i] : '';
                    $modify = str_replace($matchVars[0][$i], '$this->vars->'.$matchVars[1][$i].$array.'', $content);
                }
            }
        }
    }

}
?>
